package com.lagou.task.servlet;

import com.lagou.task.entity.Student;
import com.lagou.task.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentModifyServlet",urlPatterns = "/modify")
public class StudentModifyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("UTF-8");
        // 获取参数并创建对象
        String modifyID = request.getParameter("modifyHiddenID");

        String studentId = request.getParameter("modifyId");
        String studentName = request.getParameter("modifyName");
        String studentSex = request.getParameter("modifySex");
        String studentBirth = request.getParameter("modifyBirth");
        String studentEmail = request.getParameter("modifyEmail");
        String studentNote = request.getParameter("modifyNote");
        String studentClassName = request.getParameter("modifyClassName");
        Student modifyStudent = new Student(studentId,studentName,studentSex,studentBirth,studentEmail,studentNote,studentClassName);
        StudentService studentService = new StudentService();
        // 修改学生信息
        int i = studentService.modifyStudentService(modifyStudent);
        if (i == 1){
            request.setAttribute("modifySuccess","modifySuccess");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("list");
            requestDispatcher.forward(request, response);
            if (request.getAttribute("modifyError") != null){
                request.removeAttribute("modifyError");
            }
        }else {
            request.setAttribute("modifyError","modifyError");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("list");
            requestDispatcher.forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
